﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SimpleWebApp.Demo01
{
    public class Run
    {
        static void v1(string[] args)
        {
            var collector = new FakeMetricsCollector();
            new HostBuilder()
                .ConfigureHostConfiguration(builder => builder.AddCommandLine(args))
                .ConfigureAppConfiguration(
                    (context, builder) => builder
                    .AddJsonFile(path: "appSettings.json", optional: false)
                    // 通过如下方式指定  dotnet run /environment=staging
                    .AddJsonFile(path: $"appSettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true))
                .ConfigureServices((context, svcs) => svcs
                   .AddSingleton<IProcessorMetricsCollector>(collector)
                   .AddSingleton<IMemoryMetricsCollector>(collector)
                   .AddSingleton<INetworkMetricsCollector>(collector).AddSingleton<IMetricsDeliverer, FakeMetricsDeliverer>().AddSingleton<IHostedService, PerformanceMetricsCollector>()
                   .AddOptions()
                   .Configure<MetricsCollectionOptions>(context.Configuration.GetSection("MetricsCollection")))
                .ConfigureLogging((context, builder) => builder.AddConfiguration(context.Configuration.GetSection("Logging")).AddConsole())
                .Build()
                .Run();
        }
    }
}
